<template>
    <view class="my-path-list">
        <view class="path-list">
            <view  
                v-for="(item,index) in list" 
                :key="index"
                @tap="toAddPath(index)"
            >
            
                <view class="path-item" @tap="goConfirmOrder(item)" >
                    <view class="item-main">
                        <view class="item-name">
                            {{item.person}}
                        </view>
                        <view class="item-name">
                            {{item.phone}}
                        </view>
						
						<view class="set_icon" @click="setDefault(item.id)">
						    设置默认
						</view>
						<view class="delete_icon" @click="deleteAddress(item.id)">
						    删除
						</view>
                    </view>
                    <view class="item-main">
                        <view class="active" v-show="item.hasDefault">
                            默认
                        </view>
                        <view class="">
                            {{item.userNation}} {{item.userProvince}} {{item.userCity}} {{item.userCounty}}  {{item.detailedAddress}}
                        </view>

                    </view>
                </view>
				
            </view>
        </view>

        <view class="feedback-button" @click="addAddress()">
                新增地址
        </view>

		<view class="bottom_foot"></view>
		<commonUserTabBar :currentTab="currentTab"></commonUserTabBar>
    </view>
</template>

<script>
import global from '@/global.js'

export default {
	data() {
		return {
			//当前选项卡
			currentTab:'/pages/myMain/myAddressList',
			isSelectedPath:false,
			list:[]
		};
	},
	computed:{


	},
	onLoad(e) {
		this.listOrder();
		if (e.type === 'selectedPath') {
			this.isSelectedPath = true;
		}
	},
	methods:{
		//设置默认
		setDefault(id) {
			var token = uni.getStorageSync("token")
			console.log("caches get  token value :  ", token)
		    uni.request({
		      url: global.baseUrl + 'api/address/setup/' + id , // 接口地址
		      method:'get',
		      dataType: "json",
		      header: {
		      	'X-Token': token
		      },
			  success: (res) => {
					if(res.data.code==0){
						console.log("request data :  ", res)
						uni.reLaunch({url: '/pages/myMain/myAddressList'});
			
					}else{
						console.log("request add cart result error: " ,res.data.code)
					} 
		      },
		      fail: (error) => {
		        console.log(error)
		      }
		    })
		},
		//删除地址
		deleteAddress(id) {
			var token = uni.getStorageSync("token")
			console.log("caches get  token value :  ", token)
		    uni.request({
		      url: global.baseUrl + 'api/address/' +  id , // 接口地址
		      method:'get',
		      dataType: "json",
		      header: {
		      	'X-Token': token
		      },
			  success: (res) => {
					if(res.data.code==0){
						console.log("request data :  ", res)
						uni.reLaunch({url: '/pages/myMain/myAddressList'});
			
					}else{
						console.log("request add cart result error: " ,res.data.code)
					} 
		      },
		      fail: (error) => {
		        console.log(error)
		      }
		    })
		},
		//查询地址列表
		listOrder() {
			var token = uni.getStorageSync("token")
			console.log("caches get  token value :  ", token)
		    uni.request({
		      url: global.baseUrl + 'api/address/list', // 接口地址
		      method:'get',
		      dataType: "json",
		      header: {
		      	'X-Token': token
		      },
			  success: (res) => {
					if(res.data.code==0){
						console.log("request data :  ", res)
						this.list= res.data.data
			
					}else{
						console.log("request add cart result error: " ,res.data.code)
					} 
		      },
		      fail: (error) => {
		        console.log(error)
		      }
		    })
		},
		// 修改
		toAddPath(index){
			let pathObj = JSON.stringify({
				index:index,
				item:this.list[index]
			});
			uni.navigateTo({
				url:'../my-add-path/my-add-path?data='+pathObj
			})
		},
		// 新增
		goAddPath(){
			uni.navigateTo({
				url:'../my-add-path/my-add-path'
			})
		},
		// 返回确认订单
		goConfirmOrder(item){
			// 如果是从确认订单过来的执行
			if(this.isSelectedPath){
				// 自定义事件:页面通信
				uni.$emit('selectPathItem',item);
				// 返回上一页
				uni.navigateBack();
			}
		},
		addAddress(){
			uni.reLaunch({url: '/pages/myMain/addAddress'});
		}
	}
}
</script>

<style lang="scss">
.add-path{
    padding: 20rpx 0;
    width: 100%;
    display: flex;
    justify-content: center;
}
.add-path-btn{
    border: 2rpx solid #49bdfb;
    color: #49bdfb;
    border-radius: 30rpx;
    padding: 6rpx 60rpx;
}
.path-list{
    padding: 0 20rpx;
}
.path-item{
	margin-top: 3px;
    padding: 40rpx;
	background-color: #F8F8F8;
    border-bottom: 2rpx solid #ccc;
}
.item-main{
    padding: 8rpx 0;
    display: flex;
    align-items: center;
}
.item-name{
    padding-right: 10rpx;
}
.active{
    padding: 6rpx;
    background-color: #49bdfb;
    columns: #fff;
    border-radius: 20rpx;
    font-size: 24rpx;
    text-align: center;
}

.set_icon{
	flex: 1;
	text-align: right;
	padding: 0 12rpx;
}

.delete_icon{
	text-align: right;
	padding: 0 1rpx;
}

.feedback-button {
   text-align: center;
   padding-top: 8px;
   font-size: 32rpx;
   height: 40px;
   background-color: #E0E0E0;
}

.bottom_foot{
	height: 10rpx;
	margin-top: 300rpx;
}
</style>

